using System.Collections.Generic;
using IQIGame.Onigao.Game;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class BattleViewBindPointComponent : TComponent
    {
        //保存所有的绑点的Transform
        private Dictionary<int, Transform> _bindPointDic;
        private string _curRoleAssetName;
        private Transform _modelRoot;
        private UnitView unit;

        //模型的根节点
        private GameObject _modelRootPoint;
        private GameObject modelRootPoint
        {
            get
            {
                if (this._modelRootPoint == null)
                {

                    var rViewComp = this.unit.GetComponent<UnitViewComponent>(ETComponentType.UnitViewComponent);
                    if (rViewComp != null && rViewComp.gameObject)
                    {
                        this._modelRootPoint = rViewComp.gameObject;
                        this._modelRoot = this._modelRootPoint.transform.Find("model/Root");
                    }
                    var rTotemComp = this.unit.GetComponent<TotemViewComponent>(ETComponentType.TotemViewComponent);
                    if (rTotemComp != null)
                    {
                        this._modelRootPoint = rTotemComp.gameObject;
                    }
                }
                return this._modelRootPoint;
            }
        } 

        public override void OnCreate(TEntity entity)
        {
            this.unit = entity as UnitView;
            this._bindPointDic = new Dictionary<int, Transform>();
        }

        public Transform GetBindPoint(int pointId)
        {
            switch (pointId)
            {
                case 0:
                    if (this.modelRootPoint && !this._modelRoot) // 在这里先get以下rootPoint，保证若root需要有值就一定有
                    {
                        return this.modelRootPoint.transform;
                    }
                    return this._modelRoot;
                case -1: // 相机点
#if UNITY_EDITOR
                    if (BattleClientUtil.IsEditorMode)
                    {
                        return Camera.main.transform;
                    }
#endif
                    return ManagerCenter.UI.MainCamera.transform;
            }
            if (this._bindPointDic.TryGetValue(pointId, out var rPoint))
            {
                return rPoint;
            }
            if (this.modelRootPoint)
            {
                rPoint = BindPointUtil.GetBindPoint(pointId, this.modelRootPoint.transform);
                _bindPointDic[pointId] = rPoint;
            }
            return rPoint;
        }

        public void Refresh()
        {
            var rView = this.unit.GetComponent<UnitViewComponent>(ETComponentType.UnitViewComponent);
            if (rView != null)
            {
                var rResPath = rView.curRes.resPath;
                if (!string.IsNullOrEmpty(rResPath) && this._curRoleAssetName != rResPath)
                {
                    this._curRoleAssetName = rResPath;
                    this._bindPointDic.Clear();
                }
            }
            this._modelRootPoint = null;
        }

        protected override void OnDispose()
        {
            this._bindPointDic.Clear();
            this._curRoleAssetName = string.Empty;
            this._modelRoot = null;
        }
    }
}